﻿using System;
using System.Windows.Input;
using Addins.UI.Windows;
using WcfClientGenerator.Model.Config;
using WcfClientGenerator.UI.Resx;
using WcfClientGenerator.UI.ViewModel;

namespace WcfClientGenerator.UI
{
    /// <summary>
    /// The add-in main window
    /// </summary>
    public partial class MainWindow : WindowBase
    {
        #region Instance Constructors (Public)

        private WcfClientGeneratorConfig config = null;
        private EnvDTE80.DTE2 applicationObject = null;
        private bool closedCalled = false;

        #endregion

        #region Instance Constructors (Public)

        public MainWindow(WcfClientGeneratorConfig config, EnvDTE80.DTE2 applicationObject)
        {
            InitializeComponent();

            this.Title = AppResources.mainWindowTitle;
            this.config = config;
            this.applicationObject = applicationObject;
            ConfigurationsViewModel configurationsViewModel = new ConfigurationsViewModel(config, applicationObject, this);
            this.DataContext = configurationsViewModel;
            configurationsViewModel.RequestClose += (sender, e) =>
                {
                    if (!this.closedCalled)
                    {
                        this.closedCalled = true;
                        this.ForceClose();
                    }
                };

            if (this.config.CurrentConfiguration >= 0 && this.config.ClientConfigurations.Count > this.config.CurrentConfiguration)
            {
                this.btnGenerate.Focus();
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!this.closedCalled)
            {
                this.closedCalled = true;

                ConfigurationsViewModel configurationsViewModel = this.DataContext as ConfigurationsViewModel;
                if (configurationsViewModel != null)
                    configurationsViewModel.CloseCommand.Execute(null);
            }
            base.OnClosing(e);
        }

        private void HandleHeaderPreviewMouseDown(Object sender, MouseButtonEventArgs e)
        {
            //m_headerLastClicked = DateTime.Now;
            if (Mouse.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        #endregion
    }
}
